package com.yw.atp.service;

import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.yw.atp.model.Appointment;

@Repository
@Transactional
public class AppointmentService{
	@PersistenceContext
    private EntityManager em;
	
	public List<Appointment> getAppointments(Date beginDate,Date endDate){
		Query q = em.createNamedQuery("Appointment.getAreaAppointment");
		q.setParameter("beginDate",beginDate);
		q.setParameter("endDate",endDate);
		return (List<Appointment>)q.getResultList();
	}
	
	public Appointment save(Appointment app){
		if (app.getId() == null) {
            // new
            em.persist(app);
        } else {
            // update
            em.merge(app);
        }
		return app;
	}
	
	public void delet(Appointment app){
		em.remove(em.merge(app));
	}
}
